<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Agency extends Admin_Controller
{
	public function __construct()
	{
		parent::__construct();

		$this->load->model('agency_model');
	}

	public function index()
	{
		//get获取查询参数
		$page = intval($this->input->get('page', true)); //页数
		$where['name'] = trim($this->input->get('name', TRUE));
		$where['adminname'] = trim($this->input->get('adminname', TRUE));

		//分页
		$count = $this->agency_model->count_all_list($where); //查询总数
		$config['base_url'] = site_url('admin/agency/index?'); //基本url
		//$config['total_rows'] = $count; //总数据条数
		$config['per_page'] = 10; //每页显示多少条数据
		$this->load->library('pagination', $config);

		//根据条件查询需要显示的数据
		$agencys = array();
		if ($count)
		{
			$limit = $config['per_page']; //$this->pagination->per_page;
			$offset = $this->pagination->get_offset($page);
			$agencys = $this->agency_model->get_all_list($where, $limit, $offset, 'vieworder ASC');
		}
		$data['agencys'] = $agencys;
		$data['get'] = $where;
		$this->load->helper('form');
		$this->load->view('admin/agency_index', $data);
	}

	/**
	 * 中介编辑页面
	 */
	public function edit()
	{
		$agencyid = intval($this->input->get('agencyid', TRUE));
		$action = $this->input->get('action', TRUE);
        (!$agencyid || !is_numeric($agencyid)) && show_admin_msg('非法操作');

        $data['agency'] = $this->agency_model->get($agencyid);
        !$data['agency'] && show_admin_msg('亲，数据不存在或已删除了哦');

		$data['agencyid'] = $agencyid;
		$data['action'] = $action;

		if ($action == 'base' || $action == FALSE)
		{
			$this->load->helper('form');

			$this->load->view('admin/agency_edit_base', $data);
		}
		elseif ($action == 'data')
		{
			$data['agency'] = $this->agency_model->get_data($agencyid);
			$this->load->view('admin/agency_edit_data', $data);
		}
		elseif ($action == 'right')
		{
			$this->load->helper('form');

			$data['right'] = $this->agency_model->get_right($agencyid);
			$data['agencyid'] = $agencyid;
			$this->load->view('admin/agency_edit_right', $data);
		}
	}

	/**
	 * 基本资料更新处理
	 */
	public function do_edit_base()
	{
		//input
		$agencyid = $this->input->post('agencyid', TRUE);
		$adminname = trim($this->input->post('adminname', TRUE));
		$name = trim($this->input->post('name', TRUE));
		$telphone = trim($this->input->post('telphone', TRUE));
		$fax = trim($this->input->post('fax', TRUE));
		$address = trim($this->input->post('address', TRUE));
		//$icon = $this->input->post('icon', TRUE);
		//$bgimg = $this->input->post('bgimg', TRUE);
		$created = $this->input->post('created', TRUE);
		$updated = time();
		$isopen = intval($this->input->post('isopen', TRUE));
		$vieworder = trim($this->input->post('vieworder', TRUE));

		//数据处理
		$this->load->helper('string');
		$name && $name = pw_substr($name, 0, 100);
		$telphone && $telphone = pw_substr($telphone, 0, 32);
		$fax && $fax = pw_substr($fax, 0, 32);
		$address && $address = pw_substr($address, 0, 32);
		$created = intval(strtotime($created));
		$vieworder = (int)$vieworder;

		$this->load->model('user_model');
		$user = $this->user_model->get_by('username', $adminname);
		!$user && show_admin_msg('亲，该帐号不存在哦');

		//更新的数据(除了parentid，12个字段)
		$update['agencyid'] = $agencyid;
		$update['name'] = $name;
		$update['adminname'] = $adminname;
		$update['telphone'] = $telphone;
		$update['fax'] = $fax;
		$update['address'] = $address;
		$update['icon'] = '';
		$update['bgimg'] = '';
		$update['created'] = $created;
		$update['updated'] = time();
		$update['isopen'] = $isopen;
		$update['vieworder'] = $vieworder;

		//var_dump($update);exit;

		$this->agency_model->update($update);

		redirect('admin/agency/edit?action=base&agencyid=' . $agencyid);
	}

	/**
	 * 数据资料更新处理
	 */
	public function do_edit_data()
	{
		//input
		$agencyid = $this->input->post('agencyid', TRUE);
		$rmbnum = intval($this->input->post('rmbnum', TRUE)); //人名币余额
		$salenum = intval($this->input->post('salenum', TRUE)); //出售房总数量
		$hirenum = intval($this->input->post('hirenum', TRUE)); //出租房总数量
		$todaysalenum = intval($this->input->post('todaysalenum', TRUE)); //当日出售房数量
		$weeksalenum = intval($this->input->post('weeksalenum', TRUE)); //当周出售房数量
		$monthsalenum = intval($this->input->post('monthsalenum', TRUE)); //当月出售房数量
		$fixedsalenum = intval($this->input->post('fixedsalenum', TRUE)); //固定出售房数量
		//$lastsaletime = intval(strtotime($this->input->post('lastsaletime', TRUE))); //最后发布出售房时间
		$todayhirenum = intval($this->input->post('todayhirenum', TRUE)); //当日出租房数量
		$weekhirenum = intval($this->input->post('weekhirenum', TRUE)); //当周出租房数量
		$monthhirenum = intval($this->input->post('monthhirenum', TRUE)); //当月出租房数量
		$fixedhirenum = intval($this->input->post('fixedhirenum', TRUE)); //固定出租房数量
		//$lasthiretime = intval(strtotime($this->input->post('lasthiretime', TRUE))); //最后发布出租房时间

		//数据处理

		//更新的数据(除了3个时间字段，剩12个字段)
		$update['agencyid'] = $agencyid;
		$update['rmbnum'] = $rmbnum;
		$update['salenum'] = $salenum;
		$update['hirenum'] = $hirenum;
		$update['todaysalenum'] = $todaysalenum;
		$update['weeksalenum'] = $weeksalenum;
		$update['monthsalenum'] = $monthsalenum;
		$update['fixedsalenum'] = $fixedsalenum;
		//$update['lastsaletime'] = $lastsaletime;
		$update['todayhirenum'] = $todayhirenum;
		$update['weekhirenum'] = $weekhirenum;
		$update['monthhirenum'] = $monthhirenum;
		$update['fixedhirenum'] = $fixedhirenum;
		//$update['lasthiretime'] = $lasthiretime;

		//var_dump($update);exit;

		$this->agency_model->update_data($update);

		redirect('admin/agency/edit?action=data&agencyid=' . $agencyid);
	}

	/**
	 * 权限资料更新处理
	 */
	public function do_edit_right()
	{
		$right = $this->input->post('right', TRUE);
		$this->agency_model->replace_right($right);

        //var_dump($agencyid);exit;

        //redirect('admin/agency/edit?action=right&agencyid=' . $right['agencyid']);
	}
}

/* End of file agency.php */
/* Location: ./application/controllers/admin/agency.php */
